import Loader from "../../../../scripts/Common/Loader";
import PageBase from "../../../../scripts/frameSccript/other/page/pageBase";
import { mgrs, sers } from "../../../../scripts/frameSccript/system/OverallSystem ";
import EventManager from "../../../Common/EventManager";
import { EventType } from "../../../GameSpecial/GameEventType";
import UserService from "../../services/userService";
import gameListItemNew from "./gameListItemNew";

const { ccclass, property } = cc._decorator;

@ccclass
export default class gameListNew extends PageBase {

    public pageName: string = "更多游戏";

    @property(cc.Node)
    item: cc.Node = null

    @property(cc.Node)
    scrContent: cc.Node = null

    @property(cc.Node)
    changwanBtn:cc.Node = null

    @property(cc.Label)
    tips:cc.Label = null

    onLoad() {

    }

    start() {
        Loader.loadBundle("mainScene", (res) => {
            Loader.loadBundleRes('mainScene', 'json/mainGameList', (res) => {
                this.listData = res.json
                if(!this || !this.node)return
                this.initGame()
            })
        }, true, true);
        this._init();


        this.updateVideoCnt();
    }

    private static LookCnt = 0

    OnBtnEnterInifinityMod() {
        mgrs().soundMgr.playBtnSound();
        let changwanModel = Number(sers().localStorageSrv.get(UserService.changwanModel)) != 0;
        if (changwanModel) {
            //XXX 
            sers().uiSrv.showMessage("已经开启畅玩模式！");
            // this.close();
            this.changwanBtn.active = false
        } else {
            sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
                window['LookCnt']++;
                
                if (window['LookCnt'] >= 3) {
                    // sers().userSrv.addPower(999);
                    sers().userSrv.addPower(1999);
                    sers().localStorageSrv.set(UserService.infinityPowerModel, 1);
                    sers().localStorageSrv.set(UserService.infinityPowerDate, Date.now());
                    sers().localStorageSrv.set(UserService.changwanModel, 1);
                    sers().localStorageSrv.set(UserService.changwanModelDate, Date.now());
                    // this.close();
                    EventManager.emit(EventType.UIEvent.updataGamelist)

                }
                this.updateVideoCnt();
            }, () => {

            }, null, {
                scene: '24小时无限畅玩',
                event: '24小时无限畅玩',
              },'common_video');
        }
    }

    updateVideoCnt(){
        this.tips.string = `（${window['LookCnt']}/3）`
        this.changwanBtn.active = Number(sers().localStorageSrv.get(UserService.changwanModel)) == 1 ? false : true
    }

    listData = null

    initGame() {
        if (!this.listData) return


        this.scrContent.removeAllChildren();

        for (const key in this.listData) {
            if (Object.prototype.hasOwnProperty.call(this.listData, key)) {
                const element = this.listData[key];
                let item = cc.instantiate(this.item);
                item.parent = this.scrContent;
                item.x = 0;
                item.zIndex = 999 - element.zIndex
                item.getComponent(gameListItemNew).setData(element);
            }
        }


    }

    protected _init(): void {

        this.open();
    }

    OnBtnConfirm() {

    }

    OnBtnClose() {
        mgrs().soundMgr.playBtnSound();
        this.onlyClose();
    }

    // update (dt) {}
}
